<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增字典 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DictDataModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
import { PageWrapper } from '@/components/Page';
import { useTabs } from '@/hooks/web/useTabs';
import { useRoute } from 'vue-router';
import { BasicTable, TableAction, useTable } from "@/components/Table";
import { DeleteDictData, DeleteDictType, GetDictDataList } from "@/api/system/dict";
import { dataColoumns, dictDataSearchSchema } from "@/views/dict/dict.data";
import DictDataModal from "@/views/dict/DictDataModal.vue";
import { useModal } from "@/components/Modal";
import { notification } from "ant-design-vue";
import { useI18n } from '@/hooks/web/useI18n';
import { useDictStore } from "@/store/modules/dict";
const { t } = useI18n();
defineOptions({ name: 'DictDetail' });
const route = useRoute();
const index = route.params?.id ?? -1;
const { setTitle } = useTabs();
const [registerModal, { openModal }] = useModal();
const [registerTable, { reload }] = useTable({
  title: '字典数据列表',
  api: GetDictDataList,
  columns:dataColoumns,
  formConfig: {
    labelWidth: 120,
    schemas: dictDataSearchSchema,
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: false,
  beforeFetch: (params) => {
    params.dictType = index;
    return params;
  },
  actionColumn: {
    width: 80,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: undefined,
  },
});
// 设置标识
setTitle(`ID.${index}-字典详情`);
function handleCreate() {
  openModal(true, { isUpdate: false, dictType: index });
}
function handleEdit(record) {
  openModal(true, {
    record,
    isUpdate: true,
    dictType: index
  });
}
async function handleDelete(record) {
  //删除
  const res = await DeleteDictData(record.dictCode)
  if (res.code == 200) {
    notification.success({
      message: t('sys.delete.success'),
      description: t('sys.delete.success'),
      duration: 3,
    });
    useDictStore().delDict(record.dictType);
    await reload();
  } else {
    notification.error({
      message: t('sys.delete.fail'),
      description: res.msg,
      duration: 3,
    });
  }
}
function handleSuccess() {
  reload();
}
</script>
